package com.loong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loong.entity.Dept;
import com.loong.entity.Emp;
import com.loong.mapper.DeptMapper;
import com.loong.mapper.EmpMapper;
import com.loong.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.selectList(null);
    }

    @Override
    public Dept get(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public void insert(Dept dept) {
        deptMapper.insert(dept);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void delete(Integer id) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", id);
        empMapper.delete(queryWrapper);
        deptMapper.deleteById(id);
    }

    @Override
    public void update(Dept dept) {
        deptMapper.updateById(dept);
    }
}
